define(["js/jquery-3.2.1.min.js","js/ajax.js","js/bootstrap-select.js"],function(jq,ajax){
	
	/*var tableTpl ='<tr id="tableContent" class="tableContent"><td>'+
			  		'<input type="radio" class="linkmanRadio" id="linkman" name="linkman" value="0"  /></td>'+
			  		'<td><input type="input" class="form-control tableInput name" id="name"/></td>'+
			  		'<td><select id="sexSelect"  class="form-control tableInput sexSelect">'+
			  			'<option value ="男" selected="selected">男</option>'+
			  			'<option value ="女">女</option></select></td>'+
			  		'<td><input type="input" class="form-control tableInput department" id="department" /></td>'+
			  		'<td><input type="input" class="form-control tableInput job" id="job" /></td>'+
			  		'<td><input type="input" class="form-control tableInput phoneNumber" id="phoneNumber" /></td>'+
			  		'<td><div class="form-group"><select id="dinnerTime" title="请选择" class="selectpicker show-menu-arrow form-control dinnerTime" multiple>'+
				          '<option disabled>可多选</option><option value="25">25号</option><option value="26">26号</option>'+
						  '<option value="27">27号</option></select></div></td>'+
			  	'<td><div class="form-group"><select id="accommodationTime" title="请选择" class="selectpicker show-menu-arrow form-control accommodationTime" multiple>'+
				  		  '<option disabled>可多选</option><option value="25">25号</option><option value="26">26号</option>'+
				          '<option value="27">27号</option></select></div></td>'+
			  	'<td><select id="sightseeingSite"  class="form-control tableInput sightseeingSite"><option value ="A" selected="selected">A</option>'+
			  			'<option value ="B">B</option></select></td>'+
			  	'<td><input type="input" class="form-control tableInput remark" id="remark" />'+
			  		'<input type="input" class="id" style="display:none;" value=""  /></td>'+
			  	'<td><input type="text" class="form-control tableInput identityNumber"></td>'+
			  	'<td><input type="text" class="form-control tableInput roomNum"></td>'+
			  	'<td><select id="roomTypeBf"  class="form-control tableInput roomTypeBf"><option value ="标准间合住" selected="selected">标准间合住</option>'+
			  			'<option value ="标准间单住">标准间单住</option><option value ="单间">单间</option></select></td>'+
			  	'<td><select id="roomTypeAf"  class="form-control tableInput roomTypeAf">'+
			  			'<option value ="标准间合住" selected="selected">标准间合住</option><option value ="标准间单住">标准间单住</option>'+
			  			'<option value ="单间">单间</option></select></td></tr>';*/

	var app={

		init:function() {
			this.submit();
			this.checkout();
			this.search();
		},


		//提交
		submit:function(){
			$("#submit").click(function(){
				/*console.log($("input[name='linkman']:checked").val());*/
				//填写列表的数量
				var listNum = $(".tableContent").length;
				var listSendParameters = new Array();

				//下面都是获取多列的值，值都为一个list
				var linkmanRadioList = app.getFillContent(".linkmanRadio");
				if(!app.checkoutLinkMan(linkmanRadioList)){
					alert("请选择一位联系人");
					return false;
				}
				var nameList = app.getFillContent(".name");
				var sexSelectList = app.getFillContent(".sexSelect");
				var departmentList = app.getFillContent(".department");
				var jobList = app.getFillContent(".job");
				var phoneNumberList = app.getFillContent(".phoneNumber");
				//获取偶数：even,因为bootstrap-select会生成两个同名class
				var dinnerTimeList = app.getFillContent(".dinnerTime:even");
				var accommodationTimeList = app.getFillContent(".accommodationTime:even");
				var sightseeingSiteList = app.getFillContent(".sightseeingSite:even");
				var remarkList = app.getFillContent(".remark");
				var idList = app.getFillContent(".id");
				var identityNumberList = app.getFillContent(".identityNumber");
				var roomNumList = app.getFillContent(".roomNum");
				var roomTypeBfList = app.getFillContent(".roomTypeBf");
    			var roomTypeAfList = app.getFillContent(".roomTypeAf");


				if (app.checkoutNull(nameList)) {
					$(".name").addClass("error");
					alert("名字不允许为空");
					return false;
				}
				else if (app.checkoutNull(departmentList)) {
					$(".department").addClass("error");
					alert("部门不允许为空");
					return false;
				}
				else if (app.checkoutNull(jobList)) {
					$(".job").addClass("error");
					alert("职务不允许为空");
					return false;
				}
				else if (app.checkoutNull(phoneNumberList)) {
					$(".phoneNumber").addClass("error");
					alert("手机号码不允许为空");
					return false;
				}
				else if (app.checkoutNull(accommodationTimeList)) {
					$(".accommodationTime").addClass("error");
					alert("住宿日期不允许为空")
					return false;
				}
				else if (app.checkoutNull(accommodationTimeList)) {
					$(".accommodationTime").addClass("error");
					alert("住宿日期不允许为空")
					return false;
				}
				else if (app.checkoutNull(dinnerTimeList)) {
					$(".dinnerTimeList").addClass("error");
					alert("用餐时间不允许为空")
					return false;
				}

				//组装list<map>数据
				for (var i = 0; i < listNum; i++) {
					var SendParameters = {
					    linkman:"",//联系人
					    name:"",//姓名
					    sex:"",//性别
					    identityNumber:"",//身份证号码
					    department:"", //部门
					    job:"", //职务
					    phoneNumber:"", //手机号码
					    dinnerTime:"", //用餐时间
					    accommodationTime:"", //住宿时间
					    accommodationDay:"", //住宿天数
					    sightseeingSite:"", // 观光地点
					    remark:"", //备注
					    registerId:"",//id
					    roomNum:"",//房号
   						roomTypeBf:"",//房间类型初定
    					roomTypeAf:"",//房间类型后定
					}


					SendParameters.linkman = linkmanRadioList[i];
					//如果是联系人，则填入税号、人数
					if (linkmanRadioList[i]=1) {
	   					SendParameters.taxNumber = $("#taxNumber").val();
	    				SendParameters.peopleNumber = $("#peopleNumber").val();
					}
					else{
	   					SendParameters.taxNumber = '';
	    				SendParameters.peopleNumber = '';
					}
					SendParameters.collegeName = $("#collegeName").val();
				    SendParameters.name = nameList[i];
				    SendParameters.sex = sexSelectList[i];
				    SendParameters.department = departmentList[i];
				    SendParameters.job = jobList[i];
				    SendParameters.phoneNumber = phoneNumberList[i];
				    var dinnerTime = '';
				    //bootstarp-select的多选是是个数组，加上两个同名class的多选框，是个二维数组
				    for (var j = 0; j < dinnerTimeList[i].length; j++) {
				    	//把日期用逗号拼接
				    	dinnerTime = dinnerTime+dinnerTimeList[i][j]+','
				    }
				    SendParameters.dinnerTime = dinnerTime;
				    var accommodationTime = ''
				    for (var j = 0; j < accommodationTimeList[i].length; j++) {
				    	accommodationTime = accommodationTime+accommodationTimeList[i][j]+','
				    }
				    SendParameters.accommodationTime = accommodationTime;
				    var sightseeingSite = '';
				     for (var j = 0; j < sightseeingSiteList[i].length; j++) {
				    	sightseeingSite = sightseeingSite+sightseeingSiteList[i][j]+','
				    }
				    SendParameters.sightseeingSite = sightseeingSite;
				    SendParameters.remark = remarkList[i];
				    SendParameters.registerId = idList[i];
				    SendParameters.identityNumber = identityNumberList[i];
					SendParameters.roomNum = roomNumList[i];
					SendParameters.roomTypeBf = roomTypeBfList[i];
    				SendParameters.roomTypeAf = roomTypeAfList[i];
				    listSendParameters.push(SendParameters);
				}
				console.log(listSendParameters);
				//ajax交流
				ajax.insertRegisterUserAjax(listSendParameters).then(function (res){
					alert(res);
					window.location.href="gc2018.html"; 
				});
			});
		},


		//设置联系人radio的value为1
		changeVal:function(){
			$(".linkmanRadio").click(function(){
				$(".linkmanRadio").attr("value",0);
				$(this).attr("value","1");
			});
		},
		

		//获取填写内容
		getFillContent:function(cName){
			var result = [];
			
			$(cName).each(function(){
			    result.push($(this).val());
			});
			return result;
		},

		//校验输入框内容
		checkout:function(){
			$(".phoneNumber").change(function(){
				var result = $(this).val();
				if(!(/^1(3|4|5|7|8)\d{9}$/.test(result))){ 
					$(this).addClass("error");
			        alert("手机号码填写有误，请重填");  
			        return false; 
				}
				else{
					$(this).removeClass("error");
				}
			});

			$(".name").change(function(){
				$(".name").each(function(){
					var result = $(this).val();
					$(this).removeClass("error");
				} );
			});

			$(".department").change(function(){
				$(".department").each(function(){
					var result = $(this).val();
					$(this).removeClass("error");
					
				} );
			});

			$(".job").change(function(){
				$(".job").each(function(){
					var result = $(this).val();
					$(this).removeClass("error");
					
				} );
			});


		},

		checkoutNull:function(list){
			for (var i=0; i<list.length; i++) {
				if(list[i]==''||list[i]==null){
					return true;
				}
			}
			return false;
		},

		checkoutLinkMan:function(list){
			for(var i = 0; i < list.length; i++){
		        if('1' == list[i]){
		            return true;
		        }
		    }
		    return false;
		},


		search:function(){
			var SendParameters = {
				collegeName:'',
			}
			$("#search").click(function(){
				if ($("#searchCollege").val()==null||$("#searchCollege").val()=="") {
					alert("请输入高校名称");
					return false;
				}
				SendParameters.collegeName = $("#searchCollege").val();
				ajax.searchManageAjax(SendParameters).then(function(res){
					console.log(res);
					$(".tableContent").remove();
					for (var i = 0; i < res.length; i++) {
						var check = '';
						var sex = '<option value ="男" selected="selected">男</option>'+
					  				'<option value ="女">女</option></select>';
						var sightseeingSite = '<option value ="A" selected="selected">A</option>'+
					  							'<option value ="B">B</option></select>';
						var roomTypeBf = '<option value ="标准间合住" selected="selected">标准间合住</option>'+
					  					 '<option value ="标准间单住">标准间单住</option>'+
					  					 '<option value ="单间">单间</option></select>';
						var roomTypeAf = '<option value ="" >未选择</option>'+
										'<option value ="标准间合住" >标准间合住</option>'+
					  					 '<option value ="标准间单住">标准间单住</option>'+
					  					 '<option value ="单间">单间</option></select>';
						var result = res[i];
						if (result.linkman==1) {
							check = 'checked="checked"' ;
						}
						if (result.sex=='女') {
							sex = '<option value ="女" selected="selected">女</option>'+
					  				'<option value ="男">男</option></select>';
						}
						else{
							sex = '<option value ="男" selected="selected">男</option>'+
					  				'<option value ="女">女</option></select>';
						}
						if (result.sightseeingSite=='A') {
							sightseeingSite = '<option value ="A" selected="selected">A</option>'+
					  							'<option value ="B">B</option></select>'
						}
						else if (result.sightseeingSite=='B'){
							sightseeingSite = '<option value ="A">A</option>'+
					  							'<option value ="B" selected="selected">B</option></select>'
						}
						if (result.roomTypeBf == '标准间合住') {
							roomTypeBf ='<option value ="标准间合住" selected="selected">标准间合住</option>'+
					  		'<option value ="标准间单住">标准间单住</option>'+
					  		'<option value ="单间">单间</option></select>'
						}
						else if (result.roomTypeBf == '标准间单住') {
							roomTypeBf ='<option value ="标准间合住">标准间合住</option>'+
					  		'<option value ="标准间单住" selected="selected">标准间单住</option>'+
					  		'<option value ="单间">单间</option></select>'
						}
						else if (result.roomTypeBf == '单间') {
							roomTypeBf ='<option value ="标准间合住" >标准间合住</option>'+
					  		'<option value ="标准间单住">标准间单住</option>'+
					  		'<option value ="单间" selected="selected">单间</option></select>'
						}
						if (result.roomTypeAf == '标准间合住') {
							roomTypeAf ='<option value ="标准间合住" selected="selected">标准间合住</option>'+
					  		'<option value ="标准间单住">标准间单住</option>'+
					  		'<option value ="单间">单间</option></select>'
						}
						else if (result.roomTypeAf == '标准间单住') {
							roomTypeAf = '<option value ="标准间合住">标准间合住</option>'+
					  		'<option value ="标准间单住" selected="selected">标准间单住</option>'+
					  		'<option value ="单间">单间</option></select>'
						}
						else if (result.roomTypeAf == '单间') {
							roomTypeAf ='<option value ="标准间合住" >标准间合住</option>'+
					  		'<option value ="标准间单住">标准间单住</option>'+
					  		'<option value ="单间" selected="selected">单间</option></select>'
						}
						$("table").append(
						'<tr id="tableContent" class="tableContent">'+
				  		'<td><input type="radio" class="linkmanRadio" id="linkman" name="linkman"'+check+' value="'+result.linkman+'" /></td>'+
				  		'<td><input type="input" class="form-control tableInput name" id="name" value="'+result.name+'" /></td>'+
				  		'<td><select id="sexSelect"  class="form-control tableInput sexSelect">'+sex+'</td>'+
				  		'<td><input type="input" class="form-control tableInput department" id="department" value="'+result.department+'" /></td>'+
				  		'<td><input type="input" class="form-control tableInput job" id="job" value="'+result.job+'" /></td>'+
				  		'<td><input type="input" class="form-control tableInput phoneNumber" id="phoneNumber" value="'+result.phoneNumber+'" /></td>'+
				  		'<td><div class="form-group"><select id="dinnerTime" title="请选择" class="selectpicker show-menu-arrow form-control dinnerTime dt'+i+'"  multiple>'+
					          '<option disabled>可多选</option><option value="24">24号</option><option value="25">25号</option>'+
							  '<option value="26">26号</option></select></div></td>'+
					  	'<td><div class="form-group"><select id="accommodationTime" title="请选择" class="selectpicker show-menu-arrow form-control accommodationTime at'+i+'" multiple>'+
						  		  '<option disabled>可多选</option><option value="24">24号</option><option value="25">25号</option>'+
						          '<option value="26">26号</option></select></div></td>'+
					  	'<td><div class="form-group"><select id="sightseeingSite" title="请选择" class="selectpicker show-menu-arrow form-control sightseeingSite sss'+i+'" multiple>'+
						  		  '<option disabled>可多选</option><option value="图书馆">图书馆</option><option value="博物馆">博物馆</option>'+
						          '<option value="人民公园">人民公园</option></select></div></td>'+
					  	'<td><input type="input" class="form-control tableInput remark" id="remark" value="'+result.remark+'" />'+
					  		'<input type="input" class="id" style="display:none;" value="'+result.registerId+'" /></td>'+
					  	'<td><input type="text" class="form-control tableInput identityNumber" value ="'+result.identityNumber+'"></td>'+
					  	'<td><input type="text" class="form-control tableInput roomNum" value ="'+result.roomNum+'"></td>'+
					  	'<td><select id="roomTypeBf"  class="form-control tableInput roomTypeBf">'+roomTypeBf+'</td>'+
					  	'<td><select id="roomTypeAf" title="请选择"  class="form-control tableInput roomTypeAf">'+roomTypeAf+'</tr>'
						);
						app.changeVal();
						app.checkout();
						$('.dinnerTime').selectpicker('refresh');  
						$('.accommodationTime').selectpicker('refresh'); 
						$('.sightseeingSite').selectpicker('refresh'); 
						var dinnerTime = new Array(); //定义一数组 
						dinnerTime = result.dinnerTime.split(","); //字符分割
						var accommodationTime = new Array();
						accommodationTime = result.accommodationTime.split(",");
						var sightseeingSite = new Array();
						sightseeingSite = result.sightseeingSite.split(",");
						$('.dt'+i).selectpicker('val', dinnerTime);
						$('.at'+i).selectpicker('val', accommodationTime);
						$('.sss'+i).selectpicker('val', sightseeingSite);
					}
				});
			});
		},

	}
	app.init();
});